#include "man_100.h"
#include "ui_man_100.h"
#include<QPainter>
#include<Qtimer>
#include<QKeyEvent>
man_100::man_100(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::man_100),Start(false),direction(-1),first(0),count(0)
{
    ui->setupUi(this);
    this->setGeometry(QRect(800,380,400,400));
}

man_100::~man_100()
{
    delete ui;
}
void man_100::paintEvent(QPaintEvent *event){//绘画事件
    if(Start == false){
        Man();
    }
    if(gameover == true){
        timer->stop();
    }
    QPainter painter(this);
    //游戏背景
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(10,15,380,380);
    painter.drawPixmap(10,15,380,380,QPixmap(":/photos/background.jpg"));
    //画人
    painter.setPen(Qt::green);
    painter.setBrush(Qt::green);
    painter.drawRect(man);
    //画木块
    painter.setPen(Qt::green);
    painter.setBrush(Qt::green);
    painter.drawRect(Board);
    //显示游戏开始与游戏结束图标
    QFont font1("Courier",10);
    painter.setFont(font1);
    painter.setPen(Qt::green);
    painter.setBrush(Qt::green);
    painter.drawText(10,380,GAME);
}
void man_100::Man(){//人的初始化与活动
    Start=true;
    gameover = false;
    GAME = "Game Begins!";
    QRect rect(200,10,10,10);
    man=rect;
    timer = new QTimer(this);//计时器
    timer->start(300);//300ms
    connect(timer,SIGNAL(timeout()),SLOT(man_update()));
    Board = board();
    Board1 = board1();
    Board2 = board2();
}
void man_100::man_update(){//人的位置更新,同时木块上移,如果人踩在木块上，则一起上移
    GAME="";
    if(Board.top()==10){
        Board=board();
    }
    if(man.top()+10==Board.top()&&man.left()<Board.right()&&man.right()>Board.left()){
        if(first == 0){
            direction = 4;
            first = 1;
        }
//      else{
//          if(direction != 4){}
//          else{
//              first = 0;
//          }
    }
    switch(direction){//对移动类型分类，分别为，不在木块上的左、右移动，在木块上的左、右移动以及踩在木块上后如果不按左右键将随木块一同上升
    case 0:
        man.setLeft(man.left()-5);
        man.setRight(man.right()-5);
        man.setTop(man.top()+5);
        man.setBottom(man.bottom()+5);
        Board.setTop(Board.top()-5);
        Board.setBottom(Board.bottom()-5);
        break;
    case 1:
        man.setLeft(man.left()+5);
        man.setRight(man.right()+5);
        man.setTop(man.top()+5);
        man.setBottom(man.bottom()+5);
        Board.setTop(Board.top()-5);
        Board.setBottom(Board.bottom()-5);
        break;
    case 2:
        man.setLeft(man.left()-5);
        man.setRight(man.right()-5);
        if(man.right()<=Board.left()){
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            direction = 0;
            first = 0;
        }
        else{
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            first = 0;
        }
        Board.setTop(Board.top()-5);
        Board.setBottom(Board.bottom()-5);
        break;
    case 3:
        man.setLeft(man.left()+5);
        man.setRight(man.right()+5);
        if(man.left()>=Board.right()){
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            direction = 1;
            first = 0;
        }
        else{
            man.setTop(man.top()-5);
            man.setBottom(man.bottom()-5);
            first = 0;
        }
        Board.setTop(Board.top()-5);
        Board.setBottom(Board.bottom()-5);
        break;
    case 4:
        man.setTop(man.top()-5);
        man.setBottom(man.bottom()-5);
        Board.setTop(Board.top()-5);
        Board.setBottom(Board.bottom()-5);
        break;
    default:
        man.setTop(man.top()+5);
        man.setBottom(man.bottom()+5);
        Board.setTop(Board.top()-5);
        Board.setBottom(Board.bottom()-5);
        break;
    }
    if(man.top()<10||man.bottom()>390||man.left()<10||man.right()>390){
        gameover = true;
        GAME = "Game Over!";
    }
    count++;
    update();//更新
}
void man_100::keyPressEvent(QKeyEvent *event){//人的键盘事件
    QKeyEvent *key=(QKeyEvent*) event;
    if(man.top()+10==Board.top()&&man.left()<Board.right()&&man.right()>Board.left()){
        switch(key->key()){
        case Qt::Key_Left:
            direction = 2;
            break;
        case Qt::Key_Right:
            direction = 3;
            break;
        }
    }
    else{
        switch(key->key()){
        case Qt::Key_Left:
            direction = 0;
            break;
        case Qt::Key_Right:
            direction = 1;
            break;
        }
    }
}
QRect man_100::board(){
    int x;
    x = qrand()%25;
    QRect RECT(10+x*10,390,15,5);
    return RECT;
}
QRect man_100::board1(){
    int x;
    x = qrand()%25;
    QRect RECT(10+x*10,390,15,5);
    return RECT;
}
QRect man_100::board2(){
    int x;
    x = qrand()%25;
    QRect RECT(10+x*10,390,15,5);
    return RECT;
}
